package cc.mrbird.alarm.controller;

import cc.mrbird.alarm.domain.AlarmInfo;
import cc.mrbird.alarm.service.AlarmInfoService;
import cc.mrbird.assets.domain.AssetMana;
import cc.mrbird.assets.domain.AssetType;
import cc.mrbird.assets.service.AssetUnitService;
import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.TimeUtil;
import cc.mrbird.management.domain.ManagementInstitutions;
import cc.mrbird.sendInformation.service.IMailService;
import cc.mrbird.unit.domain.UnitInfo;
import cc.mrbird.unit.service.UnitInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "告警信息接口")
@Controller
public class AlarmInfoController extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AlarmInfoService alarmInfoService;
    @Autowired
    private UnitInfoService unitInfoService;
    @Autowired
    private IMailService iMailService;
    @Log("获取告警信息")
    @RequestMapping(value = "alarmInfo",method = RequestMethod.POST)
    /* @RequiresPermissions("alarmInfo:list")*/
    public String index() {
        return "alarmInfo/alarmInfo/alarmInfo";
    }
    @Log("获取告警信息")
    @ApiOperation(value = "获取告警信息" ,  notes="获取告警信息")
    @RequestMapping(value = "alarmInfo/list",method = RequestMethod.POST)
    /*@RequiresPermissions("alarmInfo:list")*/
    @ResponseBody
    public Map<String, Object> alarmInfoList(QueryRequest request, AlarmInfo alarmInfo) {
        return super.selectByPageNumSize(request, () -> this.alarmInfoService.findAlarmInfoList(alarmInfo, request));
    }

    @Log("获取不同状态")
    @ApiOperation(value = "获取不同状态" ,  notes="获取不同状态")
    @RequestMapping(value = "alarmInfo/findAllByType",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo  findAllByType(String nextTime){
        try {
            List<AlarmInfo> allTypeAndId = alarmInfoService.findAllTypeAndId();
            Map<String,List<AlarmInfo>> AllAlarmInfoByType = new HashMap<>();
            if(StringUtils.isNotEmpty(nextTime)){
                nextTime = TimeUtil.ComputationTime(nextTime);
            }else{
                nextTime = null;
            }
            for (AlarmInfo alarmInfo : allTypeAndId) {
                String group_concat = alarmInfo.getGroup_concat();
                List<AlarmInfo> alarmInfos = alarmInfoService.fingAllByIds(group_concat,nextTime);
                AllAlarmInfoByType.put(alarmInfo.getAlarmStatusStr(),alarmInfos);
            }
            return  ResponseBo.ok(AllAlarmInfoByType);
        } catch (Exception e) {
            return ResponseBo.error("获取不同状态信息失败,请联系管理员");
        }
    }
    @Log("根据设备类型")
    @ApiOperation(value = "根据设备类型" ,  notes="根据设备类型")
    @RequestMapping(value = "alarmInfo/findAllByAlarmType",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo  findAllByAlarmType(){
        try {
            List<AlarmInfo> allTypeAndId = alarmInfoService.findAllByAlarmType();
            Map<String,List<AlarmInfo>> AllAlarmInfoByAlarmType = new HashMap<>();
            for (AlarmInfo alarmInfo : allTypeAndId) {
                String group_concat = alarmInfo.getGroup_concat();
                List<AlarmInfo> alarmInfos = alarmInfoService.fingAllByIdsAndType(group_concat);
                AllAlarmInfoByAlarmType.put(alarmInfo.getAlarmType(),alarmInfos);
            }
            return  ResponseBo.ok(AllAlarmInfoByAlarmType);
        } catch (Exception e) {
            return ResponseBo.error("获取设备类型信息失败,请联系管理员");
        }
    }

    @ApiOperation(value = "告警信息添加接口")
    @RequestMapping(value = "alarmInfo/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo addAlarmInfo(AssetMana assetMana) {
        try {
            AlarmInfo alarmInfo = new AlarmInfo();
            //设备ID、发生时间、告警类型、设备类型、所属网格、所属单位、设备地址、告警状态、详细信息链接
            alarmInfo.setAlarmInfo_id(assetMana.getAssetMana_id());//设备ID
            alarmInfo.setCreateTime(new Date());//发生时间
            alarmInfo.setAlarmType(assetMana.getEquipment_statusStr());//告警类型
            UnitInfo unitInfo = unitInfoService.findById(assetMana.getUnit_id());
            alarmInfo.setDeviceType(assetMana.getAsset_type()); //设备类型
            alarmInfo.setGrid(unitInfo.getAttribution_grid());  //所属网格
            alarmInfo.setUnit(unitInfo.getUnit_name());//所属单位
            alarmInfo.setAddress(assetMana.getDeployment_location());//设备地址
            alarmInfo.setAlarmStatus(assetMana.getEquipment_status());//告警状态
            this.alarmInfoService.add(alarmInfo);

            //iMailService.sendSimpleMail();
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }
}
